package com.zb;

public class CpuCoreUtils {
    private static final int CPU_CORES = Runtime.getRuntime().availableProcessors();

    /**
     * 计算默认核心线程数
     * - 名称含"io"：IO密集型（CPU×2）
     * - 其他：CPU密集型（CPU核心数）
     */
    public static int getDefaultCoreSize(String threadPoolName) {
        return isIoIntensive(threadPoolName) ? CPU_CORES * 2 : CPU_CORES;
    }

    /**
     * 计算默认最大线程数
     * - IO密集型：CPU×4
     * - CPU密集型：CPU×2
     */
    public static int getDefaultMaxSize(String threadPoolName) {
        return isIoIntensive(threadPoolName) ? CPU_CORES * 4 : CPU_CORES * 2;
    }

    /**
     * 计算默认队列容量
     */
    public static int getDefaultQueueCapacity(Integer configCapacity) {
        return configCapacity != null ? configCapacity : CPU_CORES * 10;
    }

    private static boolean isIoIntensive(String threadPoolName) {
        return threadPoolName.toLowerCase().contains("io");
    }
}